home *** CD-ROM | disk | FTP | other *** search
/ Gigantic Games 2 / Gigantic Games 2.iso / pc / _m_ / minesweeper / play.cc < prev    next >
C/C++ Source or Header  |  1994-12-23  |  4KB  |  179 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. ///////////////////////////////////////////////////////////////////////////
  3. //
  4. //  AMIGA Minesweeper - The event-driven game-loop
  5. //
  6. //  (c) 1992 Hubert Feyrer (c9020@rrzc1.rz.uni-regensburg.de)
  7. //
  8. ///////////////////////////////////////////////////////////////////////////
  9.  
  10. /*
  11. ** Diverse "C"-Header
  12. */
  13. #define class _class
  14. #define template _template
  15. #define IntuitionBase IntuiBase
  16. #define GfxBase GraphBase
  17. extern "C" {
  18. # include <stdio.h>
  19. # include <stdlib.h>
  20. # include <exec/types.h>
  21. # include <exec/exec.h>
  22. # include <exec/Ports.h>
  23. # include <devices/timer.h>
  24. # include <utility/tagitem.h>
  25. # include <intuition/intuition.h>
  26. # include <intuition/intuitionbase.h>
  27. # include <intuition/gadgetclass.h>
  28. # include <graphics/gfxbase.h>
  29. # include <clib/gadtools_protos.h>
  30. # include <clib/intuition_protos.h>
  31. # include <clib/exec_protos.h>
  32. # include <math.h>
  33. }    
  34. #undef IntuitionBase
  35. #undef GfxBase 
  36. #undef class
  37. #undef template
  38.  
  39. /*
  40. ** C++-Header
  41. */
  42. #include "mine.h"
  43. #include "field.h"
  44.  
  45. void CancelIO(int idle) { if(!idle){ AbortIO(&treq.tr_node); WaitIO(&treq.tr_node); } }
  46.  
  47. //*
  48. //* Hauptschleife - das Game an sich und als solches
  49. //*
  50. GameStat play(int idle)
  51. {
  52.     IntuiMessage *imsg;
  53.     int firstgame_over=1;          // Game Over beim ersten Versuch => nochmal!
  54.     int first_touch=1;             // Timer beim ersten Touch starten
  55.  
  56.     if(idle){
  57.     OnGadget(gogad,win,NULL);
  58.     win->Flags &= ~WFLG_RMBTRAP;
  59.     }else{
  60.     SetWindowTitles(win,WIN_T,-1);
  61.     OffGadget(gogad,win,NULL);
  62.     win->Flags |= WFLG_RMBTRAP;
  63.     }
  64.  
  65.     GT_RefreshWindow(win,NULL);
  66.  
  67.     for(;;){
  68.     int isig=1<<win->UserPort->mp_SigBit;         // Intuition-Signal-Bit
  69.     int tsig=1<<tport->mp_SigBit;                 // Timer-Device-Signalbit
  70.     
  71.     int sig=Wait( isig|tsig );
  72.     if(!idle && (sig & tsig)){
  73.         GetMsg(tport);
  74.  
  75.         // Timer sofort wieder starten
  76.         treq.tr_time.tv_secs=1; 
  77.         treq.tr_time.tv_micro=0; 
  78.         SendIO(&treq.tr_node); 
  79.  
  80.         playtime++;
  81.         sprintf(ttimegad,"%04d",playtime);
  82.         GT_SetGadgetAttrs(timegad,win,NULL,
  83.                   GTTX_Text,ttimegad,
  84.                   TAG_END);
  85.         RefreshGList(timegad,win,NULL,1);
  86.     }
  87.     if(sig & isig){                               // Message von Intuition
  88.         while(( imsg=(IntuiMessage *)GT_GetIMsg(win->UserPort) )!=NULL){
  89.         switch(imsg->Class){
  90.           case IDCMP_CLOSEWINDOW:
  91.             GT_ReplyIMsg(imsg);
  92.             if(!first_touch) CancelIO(idle);
  93.             return GAME_QUIT;
  94.           case IDCMP_GADGETUP:
  95.             Gadget *gad = (Gadget *)imsg->IAddress;
  96.             if(gad==gogad){
  97.             CancelIO(idle);
  98.             return GAME_RESTART;
  99.             }
  100.             if(!idle){
  101.             if(first_touch){
  102.                 SendIO(&treq.tr_node);            // Timer starten!
  103.                 first_touch=0;
  104.             }
  105.             //*
  106.             //* Gadget kann direkt bestimmt werden.
  107.             //*
  108.             Field *f=(Field *)gad->UserData;
  109.             
  110.             switch(f->check()){
  111.               case GAME_WON:
  112.                 CancelIO(idle);
  113.                 return GAME_WON;
  114.               case GAME_LOST:
  115.                 {
  116.                 if(firstgame_over){
  117.                     // Ersatzmine legen
  118.                     int x,y;
  119.                     int i=f->i();
  120.                     int j=f->j();
  121.                     
  122.                     for(;;){
  123.                     x=rnd(lenx);
  124.                     y=rnd(leny);
  125.                     
  126.                     if((x==i && y==j) || minefield[x][y]->cnt()==-1)
  127.                       continue;
  128.                     else
  129.                       break;
  130.                     }
  131.                     
  132.                     f->cnt(0);
  133.                     minefield[x][y]->cnt(-1);
  134.                     calc_cnt();
  135.                     f->check();
  136.                 }else{
  137.                     f->open();
  138.                     CancelIO(idle);
  139.                     return GAME_LOST;
  140.                 }
  141.                 }
  142.                 break;
  143.             }
  144.             firstgame_over=0;
  145.             }
  146.             break;
  147.           case IDCMP_MOUSEBUTTONS:
  148.             if(!idle){
  149.             if(imsg->Code==MENUUP){
  150.                 //*
  151.                 //* Gadget muß aus Maus-Position ermittelt werden.
  152.                 //*
  153.                 int x=imsg->MouseX;
  154.                 int y=imsg->MouseY;
  155.                 if(x>=X0 && y>=Y0){
  156.                 int i=(x-X0)/fxs;
  157.                 int j=(y-Y0)/fys;
  158.                 if(inminefield(i,j)){
  159.                     if(first_touch){
  160.                     SendIO(&treq.tr_node);         // Timer starten!
  161.                     first_touch=0;
  162.                     }
  163.                     if(minefield[i][j]->mark()==GAME_WON){
  164.                     CancelIO(idle);
  165.                     return GAME_WON;
  166.                     }
  167.                 }
  168.                 }
  169.             }
  170.             }
  171.             break;
  172.         }
  173.         GT_ReplyIMsg(imsg);
  174.         }
  175.     }
  176.     }
  177.     /*NOTREACHED*/
  178. }
  179.